package cn.sinata.wanglin.neighbor.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import butterknife.BindView;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class ApplyFriendOrGroupActivity extends TitleActivity {

    private int type = 0;//0好友，1.群
    @BindView(R.id.et_content)
    EditText et_content;
    private String id;
    private String account;
    private String userId;

    @Override
    protected int setContentId() {
        return R.layout.activity_apply_friend_or_group;
    }

    @Override
    protected void initView() {
        super.initView();
        type = getIntent().getIntExtra("type",0);
        id = getIntent().getStringExtra("id");
        account = getIntent().getStringExtra("account");
        if (type == 1) {
            setTitle("申请进群");
            et_content.setHint("填写加群理由申请更容易通过哦～");
        } else {
            setTitle("申请添加好友");
            et_content.setHint("填写加好友理由申请更容易通过哦～");
        }

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);

        addRightButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et_content.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    content="";
                }
                showDialog();
                if (type == 1) {
                    applyGroups(content);
                } else {
                    applyFriends(content);
                }
            }
        });

    }

    void applyGroups(String content){

        HttpManager.applyGroups(userId,id,content,account)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }

    void applyFriends(String content){
        HttpManager.applyFriend(id,userId,content)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }
}
